﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.Rules
{
    public class CrmOfflineAccessStateRule : IEnableRule, IDisplayRule, IXmlBuildable
    {
        public bool Default { get; set; }
        public bool InvertResult { get; set; }
        public Enumerations.StateType Type { get; set; }

        public CrmOfflineAccessStateRule()
        {

        }

        public CrmOfflineAccessStateRule(XmlNode node)
        {
            try
            {
                if (XmlHelper.GetAttribute(node, "Default").Length > 0)
                {
                    this.Default = XmlHelper.GetAttribute(node, "Default") == "true";
                }

                if (XmlHelper.GetAttribute(node, "InvertResult").Length > 0)
                {
                    this.InvertResult = XmlHelper.GetAttribute(node, "InvertResult") == "true";
                }

                if (XmlHelper.GetAttribute(node, "Type").Length > 0)
                {
                    this.Type = (Enumerations.StateType)Enum.Parse(typeof(Enumerations.StateType), XmlHelper.GetAttribute(node, "Type"));
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode enableRuleNode)
        {
            try
            {
                XmlNode node = XmlHelper.AddNode(enableRuleNode, "CrmClientTypeRule");
                XmlHelper.AddAttribute(node, "Default", this.Default);
                XmlHelper.AddAttribute(node, "InvertResult", this.InvertResult);
                XmlHelper.AddAttribute(node, "Type", this.Type);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("CrmOfflineAccessStateRule");
                node.ContextMenuStrip = ContextMenuStripHelper.BuildStandardContextMenuStrip(node);
                node.Tag = this;

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}